const {
	Controller
} = require('uni-cloud-router')
const uniID = require('uni-id')
module.exports = class MemberController extends Controller {
	async getGiftList() {
		const {
			skuId,
			isReceived,
			pageNum,
			pageSize
		} = this.ctx.data
		
		if ((pageNum && !parseInt(pageNum)) || (pageSize && !parseInt(pageSize))) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try{
			let page = await this.service.member.getGiftList({
				skuId,
				isReceived,
				pageNum,
				pageSize
			})
			return {
				code: 200,
				result: page
			}
		}catch(e){
			console.error(e)
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async getMemberTeamList() {
		const {
			memberId,
			level,
			pageNum,
			pageSize,
			temporary,
			userId,
			keyword
		} = this.ctx.data
		if (!memberId || (level && !parseInt(level))) {
			return {
				code: 500,
				msg: "参数不对"
			}
		}
		return this.service.member.getMemberTeamList({
			memberId,
			level,
			pageNum,
			pageSize,
			temporary,
			userId,
			keyword
		})
	}
	
	async getMemberOrders() {
		let {
			memberId,
			status,
			type,
			pageNum = 1,
			pageSize = 20
		} = this.ctx.data
		if (!memberId || (status && isNaN(parseInt(status))) ||
			(type && type != 'personal' && type != 'team' && type != 'all') ||
			isNaN(parseInt(pageNum) ||
				isNaN(parseInt(pageSize)))) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		return this.service.member.getMemberOrders({
			memberId,
			status,
			type,
			pageNum,
			pageSize
		})
	}
	
	async getMemberDetail() {
		let {
			memberId
		} = this.ctx.data
		if (!memberId) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		return this.service.member.getMemberInfo({
			memberId
		})
	}
	
	async getMemberStoreInfo() {
		let {
			memberId,
			storeId
		} = this.ctx.data
		if (!storeId || !memberId) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try{
			let res = await this.service.member.getMemberStoreInfo({
				memberId,
				storeId
			})
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async getVipChargeoffRecords() {
		let {
			storeId,
			pageNum = 1,
			pageSize = 10,
			startTime,
			endTime
		} = this.ctx.data
		try{
			let res = await this.service.member.getVipChargeoffRecords({
				storeId,
				pageNum,
				pageSize,
				startTime,
				endTime
			})
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async chargeoffVipCard() {
		let {
			storeId,
			memberId,
			chargeoffType
		} = this.ctx.data
		if (!storeId || !memberId || !chargeoffType ||
			(chargeoffType !== 'enter' && chargeoffType !== 'consumeService')) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try {
			let record = await this.service.member.chargeoffVipCard({
				storeId,
				memberId,
				chargeoffType
			})
			return {
				code: 200,
				result: record
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
}
